myApp.filter('updateListFilter', [function () {
    return function (list, filter) {
        var type = ",";
        angular.forEach(filter.type, function (t) {
            if (t.check) {
                type = type + t.key + ",";
            }
        });
        if (filter.trunkName == '' && filter.role == '' && type == ',') {
            return list;
        }
        var l = [];
        angular.forEach(list, function (record) {
            if ((filter.trunkName == '' || record.trunk_name.indexOf(filter.trunkName) != -1)
                && (filter.role == '' || record.user_role == filter.role)
                && (type == ',' || type.indexOf(',' + record.key + ',') != -1)) {
                l.push(record);
            }
        });
        return l;
    };
}]);
myApp.controller('IntegrationSetController', function ($scope, $stateParams, $http, $uibModal, ngDialog, $rootScope, $interval, $sce, $filter) {
    $scope.info = {};
    $scope.info.selectIntegration = $stateParams.selectIntegration;
    $scope.info.integrationInfo = {};
    $scope.info.isHideMergePro = true;
    //集成计划内工程列表
    $scope.info.projectList = [];
    //集成计划内项目列表
    $scope.info.planList = [];
    $scope.info.showDownLoadBtn = false;
    //工程上线时间列表
    $scope.info.timeList = [];
    //交付确认检查
    $scope.info.isChecked = false;
    //交付后更新列表
    $scope.info.updateList = [];

    $scope.info.userList = [];

    $scope.info.isIntOnline = false;

    //自动抽取sql文件状态
    $scope.info.AutoDrawSqlStatus = null;

    //上线工程数量
    $scope.info.toOnlineProCount = 0;
    //依赖jar工程数量
    $scope.info.jarProCount = 0;
    //被合并工程数量
    $scope.info.merageProCount = 0;

    $scope.timer = null;

    $scope.info.filter = {};
    $scope.info.filter.trunkName = '';
    $scope.info.filter.role = '';
    $scope.info.filter.checkAll = false;
    $scope.info.filter.type = [{"check": false, "key": "to_test"}, {
        "check": false,
        "key": "cancel_to_test"
    }, {"check": false, "key": "test"},
        {"check": false, "key": "cancel_test"}, {"check": false, "key": "to_integration"}, {
            "check": false,
            "key": "cancel_to_integration"
        },
        {"check": false, "key": "can_integration"}, {"check": false, "key": "can_not_integration"}, {
            "check": false,
            "key": "integration"
        },
        {"check": false, "key": "cancel_integration"}, {"check": false, "key": "hand_over"}, {
            "check": false,
            "key": "cancel_hand_over"
        },
        {"check": false, "key": "online"}, {"check": false, "key": "change_branch"}];

    //tab页选中序号
    $scope.info.tabIndex = 0;
    $scope.info.tabs = [{"name": "工程列表", "index": "0"}, {"name": "项目列表", "index": "1"}
        , {"name": "部署说明", "index": "2"}
        , {"name": "上线时间规划", "index": "4"}, {"name": "上线备注", "index": "6"}
        , {"name": "交付确认", "index": "5"}, {"name": "转测记录", "index": "8"}, {"name": "测试记录", "index": "9"}
        // , {"name": "合并主干", "index": "7"}
        ];

    $scope.info.userRole = getUserRole();

    //集成计划上线描述信息
    $scope.info.onlineDescList = [];
    //转测记录
    $scope.info.PHORecord = [];

    $scope.$on("$destroy", function () {
        removeStorage('integrationSet_go_back');
        if ($scope.timer != null) {
            console.log("清除集成计划定时器;");
            $interval.cancel($scope.timer);
            $scope.timer = null;
        }
    });

    $scope.goUcRelease = function () {
        window.location.href = "#/menu/newucRelease/" + integration_agent_list[0] + "/" + $scope.info.integrationInfo.integration_name + "/AG201703131059304746920022/" + $scope.info.integrationInfo.integration_id;
    };

    $scope.arrayContains = function (array, obj) {
        return arrayContains(array, obj)
    };

    //tab切换
    $scope.tabClick = function (index) {
        $scope.info.tabIndex = index;
        if (index == 0) {
            $scope.getProjectList();
        } else if (index == 1) {
            $scope.getProjectPlanList();
        } else if (index == 2) {
            $scope.getProjectConfig();
        } else if (index == 4) {
            $scope.getDeployTime();
        } else if (index == 5) {
            $scope.info.filter.trunkName = '';
            $scope.info.filter.role = '';
            $scope.info.filter.checkAll = false;
            angular.forEach($scope.info.filter.type, function (type) {
                type.check = false;
            });
            doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/getIntegrationEditionChangeRecord?integration_name=' + $scope.info.selectIntegration + '&token_id=' + getTokenId(), function (data) {
                $scope.info.updateList = data.data;
            })
        } else if (index == 6) {
            $scope.getOnlineDescList();
        } else if (index == 7) {
            $scope.getProMergeRecord();
        } else if (index == 8) {
            $scope.getPHORecord();
        }
    };

    $scope.getPHORecord = function () {
        $scope.info.PHORecord = [];
        doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/getIntegrationPHORecord?token_id=' + getTokenId() + '&integration_name=' + $scope.info.selectIntegration, function (data) {
            if (data.success) {
                $scope.info.PHORecord = data.data;
            }
        });
    };

    $scope.getLength = function (array, search) {
        return $filter('filter')(array, search).length;
    };

    //获取集成计划内工程合并主干记录
    $scope.getProMergeRecord = function () {
        doBeetleGet($http, $uibModal, beetle_plan_url + '/project/getProMergeRecordList?integration_id=' + $scope.info.integrationInfo.integration_id + '&token_id=' + getTokenId(), function (data) {
            angular.forEach(data.data, function (record) {
                angular.forEach($scope.info.projectList, function (pro) {
                    if (pro.project_status != 'PRO_MERGED' && pro.project_name == record.project_name) {
                        pro.mergeInfo = record;
                        return;
                    }
                });
            });
        });
    };

    //合并分支到主干
    $scope.mergeToTrunk = function (project) {
        openDialog(ngDialog, $scope, "确定要将工程" + project.project_name + "合并到主干么？", true, function (val) {
            if (!val) {
                return;
            }
            openDialog(ngDialog, $scope, "请再次确认将工程" + project.project_name + "合并到主干？", true, function (val) {
                if (!val) {
                    return;
                }
                doBeetlePost($http, $uibModal, beetle_plan_url + '/project/mergeProToTrunk', {
                    "integration_id": $scope.info.integrationInfo.integration_id,
                    "project_name": project.project_name,
                    "type": "svn",
                    "token_id": getTokenId()
                }, function () {
                    $scope.tabClick(7);
                });
            });
        });
    };

    $scope.checkAll = function () {
        angular.forEach($scope.info.filter.type, function (type) {
            type.check = $scope.info.filter.checkAll;
        });
    };

    $scope.getOnlineDescList = function () {
        doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/getIntegrationOnlineDesc?integration_id=' + $scope.info.integrationInfo.integration_id + '&token_id=' + getTokenId(),
            function (data) {
                $scope.info.onlineDescList = data.data;
                if (arrayContains($scope.info.userRole, 'ops')) {
                    angular.forEach($scope.info.onlineDescList, function (desc) {
                        desc.desc_str = $sce.trustAsHtml(desc.desc.split('\n').join('</br>'));
                    });
                }
            });
    };

    $scope.getIntegrationInfo = function () {
        doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/getIntegrationPlanList?integration_name=' + $scope.info.selectIntegration + '&token_id=' + getTokenId(),
            function (data) {
                $scope.info.integrationInfo = data.data[0];
                if ($scope.info.integrationInfo.integration_status == 'ONLINE') {
                    $scope.info.isIntOnline = true;
                }
                $scope.getProjectList();
            });
    };

    $scope.getIntegrationInfo();

    //当前可操作的角色
    $scope.isModifiAble = function (role) {
        if ($scope.info.isIntOnline) {
            return false;
        }
        if (isUserCanOperateByRole('plan_admin', 'pm')) {
            return true;
        }
        if (role == null) {
            return false;
        } else {
            if (arrayContains(getUserRole(), role)) {
                return true;
            } else {
                return false;
            }
        }
    };

    doBeetleGet($http, $uibModal, python_url + '/api/v1/user/?withRole=true&token_id=' + getTokenId(), function (data) {
        $scope.info.userList = [];
        angular.forEach(data.data, function (user) {
            angular.forEach(user.role_list, function (role) {
                if (role.role_id == '4') {
                    $scope.info.userList.push(user);
                    return;
                }
            })
        })
    });

    $scope.getProjectList = function () {
        $scope.info.projectList = [];
        $scope.info.toOnlineProCount = 0;
        $scope.info.jarProCount = 0;
        $scope.info.merageProCount = 0;
        doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/getIntegrationProjectInfoList?isHideMergePro=' + $scope.info.isHideMergePro + '&integration_name=' + $scope.info.selectIntegration + '&token_id=' + getTokenId(),
            function (data) {
                $scope.info.projectList = data.data;
                $scope.info.projectList.sort(function (a, b) {
                    if (a.plan_name == b.plan_name) {
                        return a.trunk_name < b.trunk_name ? -1 : 1;
                    }
                    return a.plan_name < b.plan_name ? -1 : 1;
                });
                angular.forEach($scope.info.projectList, function (pro) {
                    if (pro.project_status == 'PRO_MERGED') {
                        $scope.info.merageProCount++;
                    } else if (pro.package_type == 'maven_jar') {
                        $scope.info.jarProCount++;
                    } else {
                        $scope.info.toOnlineProCount++;
                    }
                })
            });
    };

    $scope.getProjectPlanList = function () {
        doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/getProjectPlanListByIntegration?integration_name=' + $scope.info.selectIntegration + '&token_id=' + getTokenId(),
            function (data) {
                $scope.info.planList = data.data;
            });
    };

    $scope.getProjectConfig = function () {
        angular.forEach($scope.info.projectList, function (pro) {
            pro.configList = [];
        });
        doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/getProjectConfig?integration_name=' + $scope.info.selectIntegration + '&token_id=' + getTokenId(),
            function (data) {
                angular.forEach(data.data, function (config) {
                    angular.forEach($scope.info.projectList, function (pro) {
                        if (pro.project_status != 'PRO_MERGED' && pro.trunk_name == config.trunk_name) {
                            config.isDel = '0';
                            pro.configList.push(config);
                            return;
                        }
                    });
                });
                $scope.getProjectSql();
            });
    };

    $scope.editConfig = function (pro) {
        var info = pro;
        info.selectIntegration = $scope.info.selectIntegration;
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'config.html',
            controller: 'IntegrationConfigCtrl',
            size: 'lg',
            resolve: {
                info: function () {
                    return info;
                }
            }
        });
        modalInstance.result.then(function () {
        }, function (res) {
            $scope.getProjectConfig();
        });
    };

    $scope.getProjectSql = function () {
        $scope.info.showDownLoadBtn = false;
        angular.forEach($scope.info.projectList, function (pro) {
            pro.sqlList = [];
        });
        doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/getAutoDrawSqlStatus?token_id=' + getTokenId() + '&integration_id=' + $scope.info.integrationInfo.integration_id, function (data) {
            $scope.info.AutoDrawSqlStatus = data.data;
            if ($scope.info.AutoDrawSqlStatus != null && $scope.info.AutoDrawSqlStatus.status == 'running') {
                $scope.startTimer();
            } else {
                if ($scope.timer != null) {
                    console.log("清除集成计划定时器;");
                    $interval.cancel($scope.timer);
                    $scope.timer = null;
                }
            }
            doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/getProjectSql?integration_name=' + $scope.info.selectIntegration + '&token_id=' + getTokenId(), function (data) {
                angular.forEach($scope.info.projectList, function (pro) {
                    angular.forEach(data.data, function (sql) {
                        if (pro.project_status != 'PRO_MERGED' && pro.trunk_name == sql.trunk_name) {
                            sql.isDel = false;
                            $scope.info.showDownLoadBtn = true;
                            pro.sqlList.push(sql);
                            pro.sqlRecord = sql.record;
                            return;
                        }
                    });
                    pro.sqlDesc = "";
                    if ($scope.info.AutoDrawSqlStatus != null && $scope.info.AutoDrawSqlStatus.projects != undefined) {
                        angular.forEach($scope.info.AutoDrawSqlStatus.projects, function (status) {
                            if (status.project != undefined && pro.trunk_name == getTrunkName(status.project)) {
                                if (!status.status) {
                                    pro.sqlDesc = status.desc;
                                } else {
                                    pro.sqlDesc = "抽取成功";
                                }
                            }
                        });
                    }
                });
                doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/getDrawSqlRecord?integration_name=' + $scope.info.selectIntegration + '&token_id=' + getTokenId(), function (data) {
                    angular.forEach($scope.info.projectList, function (pro) {
                        var list = [];
                        angular.forEach(data.data, function (sql) {
                            if (pro.project_status != 'PRO_MERGED' && pro.trunk_name == sql.trunk_name) {
                                list.push(sql);
                                return;
                            }
                        });
                        pro.sqlRecord = list;
                    });
                });
            });
        });
    };

    $scope.getToIntegrationRecord = function (project) {
        $rootScope.editionIntegrationTemplate(project.project_name, 'SEARCH', function () {
        });
    };

    $scope.startTimer = function () {
        if ($scope.timer != null) {
            return;
        }
        console.log('启动集成计划定时器');
        $scope.timer = $interval(function () {
            $scope.getProjectSql();
        }, 20000);
    };

    $scope.editSql = function (pro) {
        var info = pro;
        info.selectIntegration = $scope.info.selectIntegration;
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'sql.html',
            controller: 'IntegrationSqlCtrl',
            size: 'lg',
            resolve: {
                info: function () {
                    return info;
                }
            }
        });
        modalInstance.result.then(function () {
        }, function () {
            $scope.getProjectSql();
        });
    };
    $scope.addPlan = function () {
        var info = {};
        info.projectList = $scope.info.projectList;
        info.integrationInfo = $scope.info.integrationInfo;
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'plan.html',
            controller: 'PlanCtrl',
            size: 'lg',
            keyboard: false,
            backdrop: 'static',
            resolve: {
                info: function () {
                    return info;
                }
            }
        });
        modalInstance.result.then(function () {
        }, function (reason) {
            if (reason == 'save') {
                $scope.getProjectList();
            }
        });
    };

    $scope.uploadSql = function (pro) {
        var uploadUrl = beetle_plan_url + "/integration/uploadSqlFromProject";
        var data = {
            integration_name: $scope.info.selectIntegration,
            project_name: pro.project_name
        };

        $rootScope.FileUploadTemplate(uploadUrl, ['sql'], data, true, function () {
            $scope.getProjectSql();
        });
    };

    //上传上线备注文件
    $scope.uploadOnlineDescFile = function (desc) {
        var uploadUrl = beetle_plan_url + "/integration/uploadOnlineDescFile";
        var data = {
            integration_name: $scope.info.selectIntegration,
            desc_id: desc.id
        };

        $rootScope.FileUploadTemplate(uploadUrl, ['xml', 'doc', 'txt', 'docx'], data, false, function () {
            $scope.tabClick(6);
        });
    };

    $scope.delOnlineDescFile = function (desc) {
        openDialog(ngDialog, $scope, "确定要删除文件" + desc.file_name + "么？", true, function (val) {
            if (!val) {
                return;
            }
            doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/removeOnlineDescFile?token_id=' + getTokenId() + '&integration_name=' + $scope.info.selectIntegration + '&desc_id=' + desc.id, function (data) {
                if (!data.success) {
                    openDialogAlert(ngDialog, $scope, data.msg);
                } else {
                    $scope.tabClick(6);
                }
            });
        });
    };

    $scope.getDeployTime = function () {
        $scope.info.timeList = [];
        var list = [];
        angular.forEach($scope.info.projectList, function (pro) {
            if (!arrayContains(list, pro.trunk_name)) {
                list.push(pro.trunk_name);
            }
        });

        doBeetleGet($http, $uibModal, beetle_plan_url + '/project/getProjectDeployTime?integration_id=' + $scope.info.integrationInfo.integration_id + '&token_id=' + getTokenId(), function (data) {
            $scope.info.timeList = data.data;
            $scope.info.timeList.sort(function (a, b) {
                return a.zone_code < b.zone_code ? -1 : 1;
            });
        });
    };
    //撤下项目GET /integration/disableProjectPlanInIntegration
    $scope.cancelPlan = function (plan) {
        doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/disableProjectPlanInIntegration?token_id=' + getTokenId() + '&integration_name=' + $scope.info.selectIntegration + '&plan_name=' + plan.plan_name, function (data) {
            $scope.getProjectPlanList();
        });
    };

    $scope.downloadFile = function (fileName, filePath) {
        window.location.href = beetle_plan_url + "/integration/downloadFile?file_name=" + fileName + "&file_path=" + filePath + "&token_id=" + getTokenId();
    };
    $scope.downloadFilePackage = function () {
        window.location.href = beetle_plan_url + "/integration/downloadSqlPackage?integration_name=" + $scope.info.selectIntegration + "&token_id=" + getTokenId();
    };
    $scope.checkConfig = function (config) {
        var list = [];
        list.push(config);
        doBeetlePost($http, $uibModal, beetle_plan_url + '/integration/updateProjectConfig', {
            token_id: getTokenId(),
            integration_name: $scope.info.selectIntegration,
            config_list: JSON.stringify(list)
        }, function () {
        });
    };
    $scope.checkSql = function (sql) {
        doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/checkSql?token_id=' + getTokenId() + '&integration_id=' + $scope.info.integrationInfo.integration_id
            + '&trunk_name=' + sql.trunk_name + '&file_name=' + sql.file_name + '&status=' + sql.is_dba_check, function () {
        });
    };
    $scope.userSelect = function (time) {
        time.deployer_account = time.userInfo.account;
        time.deployer_realname = time.userInfo.realname;
    };
    $scope.saveUser = function (time) {
        time.isModify = false;
        doBeetleGet($http, $uibModal, beetle_plan_url + '/project/updateProjectDeployUser?token_id=' + getTokenId() + '&realname=' + time.deployer_realname + '&integration_id=' + $scope.info.integrationInfo.integration_id
            + '&trunk_name=' + time.trunk_name + '&zone_code=' + time.zone_code + '&account=' + time.deployer_account, function () {
        });
    };
    $scope.handOver = function () {
        doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/handOverIntegration?token_id=' + getTokenId() + '&integration_id=' + $scope.info.integrationInfo.integration_id, function () {
            $scope.getIntegrationInfo();
            $scope.tabClick(0);
        });
    };
    $scope.cancelHandOver = function (project) {
        openDialog(ngDialog, $scope, "确定要取消交付工程" + project.project_name + "么？", true, function (val) {
            if (!val) {
                return;
            }
            doBeetleGet($http, $uibModal, beetle_plan_url + '/project/updateProjectStatus?token_id=' + getTokenId() + '&project_name=' + project.project_name + '&new_status=BE_CANCEL', function () {
                $scope.tabClick(0);
            });
        });
    };
    //是否允许集成GET /project/updateProjectEdition
    $scope.proToIntegration = function (project) {

        var saveFun = function (data) {
            $scope.tabClick(0);
        };
        $rootScope.editionIntegrationTemplate(project.project_name, 'CAN_INTEGRATION', saveFun);
    };

    //抽取工程sql文件
    $scope.drawSqlFromProject = function () {
        openDialog(ngDialog, $scope, "确定要抽取工程sql文件吗,抽取后将覆盖之前的文件？", true, function (val) {
            if (!val) {
                return;
            }
            doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/autoDrawSqlFromProject?integration_name=' + $scope.info.selectIntegration + '&token_id=' + getTokenId(), function (data) {
                if (data.success) {
                    openDialogAlert(ngDialog, $scope, "调用成功，请稍后查询状态!");
                    $scope.getProjectSql();
                } else {
                    openDialogAlert(ngDialog, $scope, data.msg);
                }
            });
        });
    };

    $scope.getEditionPath = function (project, editionNum) {
        if (!(('maven_war' == project.package_type
            || 'php' == project.package_type
            || 'ant_android' == project.package_type
            || 'spring_boot_jar' == project.package_type)
            && project.to_integration_edition != ''
            && project.to_integration_edition != null)) {
            return false;
        }
        var path = 'beetle/' + project.trunk_name + "/" + project.project_name + "/" + editionNum + "/";
        if ('maven_war' == project.package_type) {
            project.tip = path + project.package_name + ".war";
        } else if ('php' == project.package_type
            || 'ant_android' == project.package_type) {
            project.tip = path + project.package_name + ".zip";
        } else if ('spring_boot_jar' == project.package_type) {
            project.tip = path + project.package_name + ".jar";
        }
        return true;
    };

    //保存集成计划上线描述
    $scope.saveIntegrationDesc = function (desc) {
        openDialog(ngDialog, $scope, "确定要保存当前描述么？", true, function (val) {
            if (!val) {
                return;
            }
            LoadModal($uibModal);
            var param = {
                token_id: getTokenId(),
                integration_id: $scope.info.integrationInfo.integration_id,
                desc: desc.desc
            };
            if (desc.id != null) {
                param.desc_id = desc.id;
            }
            $http.post(beetle_plan_url + '/integration/saveIntegrationOnlineDesc', param).success(function (data) {
                if (data.success) {
                    openDialogAlert(ngDialog, $scope, "保存成功");
                } else {
                    openDialogAlert(ngDialog, $scope, "保存失败," + data.msg);
                }
            }).finally(function () {
                $scope.getOnlineDescList();
                closeModal();
            });
        });
    };

    $scope.addOnlineDesc = function () {
        var obj = {};
        obj.integration_id = $scope.info.integrationInfo.integration_id;
        obj.real_name = getUserName();
        obj.id = null;
        obj.desc = '';
        $scope.info.onlineDescList.push(obj);
    };

    $scope.toPlan = function (plan) {
        if (plan.isEmergency == '0') {
            window.location.href = "#/menu/emergencyInfo/" + plan.plan_id;
        } else {
            window.location.href = "#/menu/plan_ProjectList/" + plan.plan_id;
        }
    };

    $scope.sqlAnalyse = function (info) {
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'sqlAnalyse.html',
            controller: 'SqlAnalyseCtrl',
            size: 'lg',
            resolve: {
                info: function () {
                    return info;
                }
            }
        });
    };
    $scope.sqlRecord = function (pro) {
        var info = {};
        info.project_name = pro.project_name;
        info.sqlRecord = pro.sqlRecord;
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'sqlRecord.html',
            controller: 'SqlRecordCtrl',
            size: 'lg',
            resolve: {
                info: function () {
                    return info;
                }
            }
        });
    };
});
myApp.controller('IntegrationConfigCtrl', function ($scope, $uibModalInstance, $http, info, $uibModal, ngDialog) {
    $scope.info = info;

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };

    $scope.submit = function () {
        var flag = true;
        angular.forEach($scope.info.configList, function (config) {
            if (config.isDel == '0' && (config.file_name == null || config.file_name == '' || config.desc == null || config.desc == ''
                || config.key == null || config.key == '')) {
                flag = false;
            }
        });

        if (!flag) {
            openDialogAlert(ngDialog, $scope, "配置项文件名、描述、key不能为空");
            return
        }

        LoadModal($uibModal);
        $http.post(beetle_plan_url + '/integration/updateProjectConfig', {
            token_id: getTokenId(),
            integration_name: $scope.info.selectIntegration,
            config_list: JSON.stringify($scope.info.configList)
        }).success(function () {
            $uibModalInstance.dismiss('submit');
        }).finally(function () {
            closeModal();
        });
    };

    $scope.add = function () {
        var obj = {};
        obj.trunk_name = info.trunk_name;
        obj.type = '1';
        //默认为0   未删除
        obj.isDel = '0';
        obj.is_ops_check = '1';
        obj.desc = null;
        obj.key = null;
        obj.value = null;
        obj.file_name = null;
        obj.config_type = 'URL';
        obj.value_from = '参考代码';
        info.configList.push(obj)
    };

    $scope.del = function (index) {
        //删除改为1
        $scope.info.configList[index].isDel = '1';
    };
});
myApp.controller('IntegrationSqlCtrl', function ($scope, $uibModalInstance, $http, info, $uibModal) {
    $scope.info = info;

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
    $scope.downloadFile = function (fileName, filePath) {
        window.location.href = beetle_plan_url + "/integration/downloadFile?file_name=" + fileName + "&file_path=" + filePath + "&token_id=" + getTokenId();
    };

    $scope.del = function (index, trunkName, fileName) {
        $scope.info.sqlList[index].isDel = true;
        LoadModal($uibModal);
        $http.get(beetle_plan_url + '/integration/removeSqlFile?token_id=' + getTokenId() + '&integration_name=' + $scope.info.selectIntegration + '&filename=' + fileName + '&trunk_name=' + trunkName).success(function (data) {
        }).finally(function () {
            closeModal();
        });
    };
});
myApp.controller('PlanCtrl', function ($scope, $uibModalInstance, $http, info, ngDialog, $uibModal) {
    $scope.info = info;
    $scope.info.planList = [];
    //集成计划内现有工程列表
    $scope.info.intPro = ",";
    $scope.info.selectPlan = {};

    $scope.info.errMsg = '';

    $http.get(beetle_plan_url + '/project_plan/getPlanList?hasProList=true&token_id=' + getTokenId() + '&isShowOnline=false' + '&start_date=' + $scope.info.integrationInfo.integration_date).success(function (data) {
        angular.forEach($scope.info.projectList, function (pro) {
            $scope.info.intPro = $scope.info.intPro + pro.trunk_name + ','
        });
        angular.forEach(data.data, function (plan) {
            if (plan.plan_status == 'TO_INTEGRATION') {
                $scope.info.planList.push(plan);
            }
        });
        $scope.info.selectPlan = $scope.info.planList[0];
        $scope.planSelect();
    });


    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
    $scope.save = function () {
        openDialog(ngDialog, $scope, "确定要集成该工程吗？", true, function (val) {
            if (!val) {
                return;
            }
            LoadModal($uibModal);
            $http.post(beetle_plan_url + '/integration/addPlanForIntegration?integration_id=' + $scope.info.integrationInfo.integration_id + '&token_id=' + getTokenId() + '&plan_id=' + $scope.info.selectPlan.plan_id).success(function (data) {
                closeModal();
                $uibModalInstance.dismiss('save');
            });
        });
    };
    $scope.createPlan = function () {
        $scope.cancel();
        window.location.href = "#/menu/plan_Guide/";
    };

    $scope.planSelect = function () {
        $scope.info.errMsg = '';
        var trunks = '';
        if (typeof $scope.info.selectPlan == 'undefined' || typeof $scope.info.selectPlan.projects == 'undefined') {
            return;
        }
        angular.forEach($scope.info.selectPlan.projects, function (pro) {
            if ($scope.info.intPro.indexOf(',' + pro.trunk_name + ',') != -1) {
                trunks = trunks + pro.trunk_name + ',';
            }
        });
        if (trunks != '') {
            $scope.info.errMsg = '当前项目内存在集成计划中的工程，不能重复集成。' + trunks.substring(0, trunks.length - 1);
            return;
        }
        $scope.info.errMsg = null;
    };
});
myApp.controller('SqlAnalyseCtrl', function ($scope, $uibModalInstance, info) {
    $scope.info = info;

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
});

myApp.controller('SqlRecordCtrl', function ($scope, $uibModalInstance, info) {
    $scope.info = info;
    $scope.info.selectRecord = null;
    $scope.info.msg = '';
    $scope.info.recordList = [];

    angular.forEach($scope.info.sqlRecord, function (r) {
        r.click = false;
    });

    $scope.clickRecord = function (record) {
        angular.forEach($scope.info.sqlRecord, function (r) {
            r.click = false;
        });
        record.click = true;
        $scope.info.selectRecord = record;
        $scope.info.msg = '';
        $scope.info.recordList = [];
        if (record.change_desc.indexOf('#') == -1) {
            $scope.info.msg = "没有sql文件";
        } else {
            angular.forEach(record.change_desc.split('@'), function (re) {
                var obj = {};
                obj.file_name = re.split('#')[0];
                obj.desc = re.split('#')[1];
                obj.info = {};
                if (re.split('#').length == 3) {
                    obj.info = jQuery.parseJSON(re.split('#')[2]);
                }
                $scope.info.recordList.push(obj);
            })
        }
    };

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
});